package cn.zhengzhou.springbootmybatis.demo;

import cn.zhengzhou.springbootmybatis.pojo.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class StreamDemo {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张无忌");
        list.add("周芷若");
        list.add("赵敏");
        list.add("张强");
        list.add("张三丰");
        list.add("张翠山");
        List<String> list1 = list.stream().filter(s -> s.startsWith("张"))
                .filter(s -> s.length() == 3)
                .collect(Collectors.toList());
        System.out.println(list1);

        ArrayList<Double> s = new ArrayList<>();
        s.add(12.0);
        s.add(88.0);
        s.add(88.0);
        s.add(54.0);
        s.add(54.0);
        s.add(66.0);
        //s.stream().sorted((a,b)->b.compareTo(a)).forEach(System.out::println);
        s.stream().sorted((a,b)->Double.compare(a,b))
                .limit(5) //保留前两个
                .distinct()  //去重
                .skip(2)  //跳过前两个
                .forEach(System.out::println);
        s.stream().map(a->"加10分钟："+(a+10)).forEach(System.out::println);

        ArrayList<Student> lists = new ArrayList<>();
        lists.add(new Student("25", "张胜男", 22, "男"));
        lists.add(new Student("5", "行行", 32, "男"));
        lists.add(new Student("9", "户次", 16, "男"));
        lists.add(new Student("16", "巴拉", 26, "男"));
        lists.add(new Student("2", "耳语", 11, "男"));

        //输出年龄最大的学生
        Optional<Student> max = lists.stream().max((a, b) -> Integer.compare(a.getAge(), b.getAge()));
        Student student = max.get();
        System.out.println(student);
        lists.stream().min((a, b) -> Integer.compare(a.getAge(), b.getAge())).ifPresent(System.out::println);
        Map<Integer, String> collect = lists.stream().collect(Collectors.toMap(Student::getAge, Student::getName));
        System.out.println(collect);


    }
}
